package com.masterrabbit.mcslf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.masterrabbit.mcslf.contans.Constants;
import com.masterrabbit.mcslf.pojo.DailyFlow;
import com.masterrabbit.mcslf.pojo.Orders;
import com.masterrabbit.mcslf.service.DailyFlowService;
import com.masterrabbit.mcslf.mapper.DailyFlowMapper;
import com.masterrabbit.mcslf.service.MerchantsService;
import com.masterrabbit.mcslf.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author hasee
 * @description 针对表【daily_flow(每日流水)】的数据库操作Service实现
 * @createDate 2025-03-20 10:36:18
 */
@Service
public class DailyFlowServiceImpl extends ServiceImpl<DailyFlowMapper, DailyFlow>
        implements DailyFlowService{
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    MerchantsService merchantsService;
    @Autowired
    DailyFlowMapper dailyFlowMapper;
    @Override
    public void updataLock(Orders orders, String currentStore) throws RuntimeException {
        boolean flag = true;
        while (flag){
            while (flag){
                flag =! redisTemplate.opsForValue().setIfAbsent(Constants.DAILY_FLOW_VERSION+orders.getMerchantId(), 1);
            }
        }
        //        查询当日流水是否存在
        QueryWrapper<DailyFlow> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.lambda()
                .eq(DailyFlow::getMerchantId, currentStore)
                .like(DailyFlow::getRecoedDate, TimeUtil.getcruntTime("YYYY-MM-dd"));
        DailyFlow dailyFlow = dailyFlowMapper.selectOne(objectQueryWrapper);

//        不存在创建
        if (dailyFlow==null){
            System.out.println("不存在创建");
            DailyFlow dailyFlow1 = new DailyFlow();
            dailyFlow1.setRecoedDate(new Date());
            dailyFlow1.setMerchantId(Integer.valueOf(currentStore));
            dailyFlowMapper.insert(dailyFlow1);
        }
        DailyFlow dailyFlowByTime = null;
        try {
            QueryWrapper<DailyFlow> dailyFlowQueryWrapper = new QueryWrapper<>();
            dailyFlowQueryWrapper.lambda().eq(DailyFlow::getMerchantId, currentStore).like(DailyFlow::getRecoedDate, TimeUtil.getcruntTime("YYYY-MM-dd"));
            dailyFlowByTime = dailyFlowMapper.selectOne(dailyFlowQueryWrapper);
            dailyFlowByTime.setDayOrderNum(dailyFlowByTime.getDayOrderNum()+1);
            dailyFlowByTime.setPriceAmount(dailyFlowByTime.getPriceAmount().add(orders.getPaidAmount()));
            String serviceCategory = orders.getServiceCategory();
            switch (serviceCategory){
                case "1":
                    System.out.println("修车");
                    dailyFlowByTime.setRepairsNum(dailyFlowByTime.getRepairsNum()+1);
                    dailyFlowByTime.setRepairsPrice(dailyFlowByTime.getRepairsPrice().add(orders.getPaidAmount()));
                    break;
                case "2":
                    System.out.println("保养车");
                    dailyFlowByTime.setMaNum(dailyFlowByTime.getMaNum()+1);
                    dailyFlowByTime.setMaintenancePrice(dailyFlowByTime.getMaintenancePrice().add(orders.getPaidAmount()));
                    break;
                case "3":
                    System.out.println("洗车");
                    dailyFlowByTime.setWashNum(dailyFlowByTime.getWashNum()+1);
                    dailyFlowByTime.setWashPrice(dailyFlowByTime.getWashPrice().add(orders.getPaidAmount()));
                    break;
                case "4":
                    System.out.println("加油");

                    dailyFlowByTime.setRefuNum(dailyFlowByTime.getRefuNum()+1);
                    dailyFlowByTime.setRefuelingPrice(dailyFlowByTime.getRefuelingPrice().add(orders.getPaidAmount()));
                    break;
                default:
                    System.out.println("数据错误");
                    throw new RuntimeException("请核销正确订单");
            }
            dailyFlowMapper.updateById(dailyFlowByTime);
            merchantsService.updataShopBalance(orders.getMerchantId(),orders.getPaidAmount(),Constants.INCREASE);
        } catch (RuntimeException e) {
            System.out.println("插入错误");
            System.out.println(e.getMessage());
            throw new RuntimeException(e);
        }finally {
            redisTemplate.delete(Constants.DAILY_FLOW_VERSION+orders.getMerchantId());
        }


    }

}




